﻿using JZ_UDI.BLL;
using JZ_UDI.CommonHelper;
using JZ_UDI.DataAccess;
using JZ_UDI.DataAccess.DataEntity;
using NLog;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JZ_UDI
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            
            InitializeComponent();
            lblTitle.Font = new Font(Application.StartupPath + "\\Assets\\font\\AlimamaShuHeiTi-Bold", 16F, FontStyle.Bold);
        }

        private Point mPoint;

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            mPoint = new Point(e.X, e.Y);
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                User myUser = new User();
                myUser.UserName = txtUid.InputText.Trim();
                myUser.Password = MD5Helper.MD5Encrypt32(txtPwd.InputText.Trim());
                User _user = new LoginBLL().isLogin(myUser);
                if (_user == null)
                {
                    MessageBox.Show("用户名或密码错误!");
                    Global.SaveLogger(LogLevel.Info, Global.logger.Name, "用户名或密码错误!");
                    return;
                }
                else
                {
                    if (myUser.Password != _user.Password)
                    {
                        MessageBox.Show("密码不正确","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                        return;
                    }
                    Global.UserInfo = _user;

                    //记录当前用户
                    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    config.AppSettings.Settings["CurrentUser"].Value = _user.UserName;
                    config.Save(ConfigurationSaveMode.Modified);
                    ConfigurationManager.RefreshSection("appSettings");
                   

                     
                    using (var context = new EFDbContext())
                    {
                        //读取厂商代码
                        string vc = context.VendorCodes.ToList().Find(o => o.isDeleted == 0 && o.ActivatedState.Equals("是")).Vendor_Code;
                        Global.VendorCode = vc;

                        //读取权限关系
                        Global.UserPrivileges = context.UserPrivileges.ToList();
                        //读取权限列表
                        Global.PriList = context.privilegeLists.ToList();

                    }

                    frmMain frmMain = new frmMain();
                    frmMain.Show();
                    this.Hide();
                    Global.SaveLogger(LogLevel.Info, Global.logger.Name, "登陆成功! ");
                }
            }
            catch (Exception ex)
            {
                Global.SaveLogger(LogLevel.Error, Global.logger.Name, ex.Message);
            }
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            txtUid.InputText = ConfigurationManager.AppSettings["CurrentUser"];
        }


    }
}
